package main

import (
	"fmt"
	"sync"
	"time"
)

func testFunc(wg *sync.WaitGroup, id int) {
	defer wg.Done()
	fmt.Printf("%v goroutine start...\n", id)
	time.Sleep(2 * time.Second)
	fmt.Printf("%v goroutine end...\n", id)
}

//等待goroutine组结束
func main() {
	var wg sync.WaitGroup
	const N = 3
	wg.Add(3)
	for i := 0; i < N; i++ {
		go testFunc(&wg, i)
	}

	fmt.Println("等待所有goroutine完成")
	wg.Wait()
	fmt.Println("所有goroutine完成\"")
}
